﻿using System;
using System.Collections.Generic;
using System.Text;
using RegionInterface;
using Common;
using UnityEngine;


public partial class AvatarModule : INetwork, IAvatarModule
{
    public void BroadcastPosition(float y, float z, byte para)
    {
        AppInterface.NetworkModule.SendNetworkMessage(ServerType.RegionServer, delegate(DataObject msg)
        {
            Log.Debug("BroadcastPosition:" + y + "," + z + "," + para);
            msg.WriteByte(GameMessageType.MOVEMENT);
            msg.WriteInt32(Owner.Id);
            msg.WriteByte(para);
            msg.WriteFloat(y);
            msg.WriteFloat(z);
        });
    }
    public void BroadcastAction(UInt16 actionId)
    {
        AppInterface.NetworkModule.SendNetworkMessage(ServerType.RegionServer, delegate(DataObject msg)
        {
            Log.Debug("BroadcastAction:" + actionId.ToString());
            msg.WriteByte(GameMessageType.ACTION);
            msg.WriteInt32(Owner.Id);
            msg.WriteInt16(actionId);
        });
    }
    public void BroadcastOnBeat(int playerId, ushort beat)
    {
        AppInterface.NetworkModule.SendNetworkMessage(ServerType.RegionServer, delegate(DataObject msg)
        {
            Log.Debug("BroadcastOnBeat player:" + playerId + ", onbeat:" + beat.ToString());
            msg.WriteByte(GameMessageType.ONBEAT);
            msg.WriteInt32(Owner.Id);
            msg.WriteInt32(playerId);
            msg.WriteInt16(beat);
        });
    }
    #region IAvatarModule Members
    public void AddPlayer(bool isOwner, PlayerData p)
    {
        if (isOwner)
        {
            Owner = new AvatarHandle(this, isOwner, p.PId, p.PNick, p.Para);
            Owner.LoadComplete += delegate()
            {
                iPlayerLogin(Owner as IAvatarHandle);
                if (Define.NoNetwork) return;
                AppInterface.NetworkModule.GetRegionPlayers();
            };
        }
        else
        {
            AvatarHandle player = new AvatarHandle(this, isOwner, p.PId, p.PNick, p.Para);
            ScenePlayers.Add(player);
            player.LoadComplete += delegate()
            {
                iPlayerLogin(player as IAvatarHandle);
                AppInterface.NetworkModule.SendNetworkMessage(ServerType.RegionServer, delegate(DataObject msg)
                {
                    msg.WriteByte(GameMessageType.POSITION_REQUEST);
                    msg.WriteInt32(Owner.Id);
                    msg.WriteInt32(player.Id);
                });
                AppInterface.NetworkModule.GetRegionNextPlayer();
            };
        }
    }
    public void DeletaPlayer(int p)
    {
        for (int i = 0; i < ScenePlayers.Count; i++)
        {
            if (p == ScenePlayers[i].Id)
            {
                iPlayerLogout(ScenePlayers[i] as IAvatarHandle);
                AvatarHandle tmp = ScenePlayers[i];
                DeletePlayer(ref tmp);
                ScenePlayers.RemoveAt(i);
                break;
            }
        }
    }
    public event Action<IAvatarHandle> OnIPlayerLogin;
    private void iPlayerLogin(IAvatarHandle p)
    {
        if (OnIPlayerLogin != null) OnIPlayerLogin(p);
    }
    public event Action<IAvatarHandle> OnIPlayerLogout;
    private void iPlayerLogout(IAvatarHandle p)
    {
        if (OnIPlayerLogout != null) OnIPlayerLogout(p);
    }
    List<IAvatarHandle> IAvatarModule.SceneIPlayers
    {
        get
        {
            List<IAvatarHandle> plays = new List<IAvatarHandle>();
            foreach (AvatarHandle avatar in ScenePlayers)
            {
                plays.Add(avatar as IAvatarHandle);
            }
            return plays;
        }
    }
    public IAvatarHandle FindIPlayer(int id)
    {
        return FindPlayer(id) as IAvatarHandle;
    }
    IAvatarHandle IAvatarModule.IOwner
    {
        get { return Owner as IAvatarHandle; }
    }
    #endregion

    #region INetwork Members
    public ModuleType Type
    {
        get { return ModuleType.Avatar; }
    }
    public void OnNetMessage(ServerType type, DataObject msg)
    {
        if (type == ServerType.RegionServer)
        {
            switch (msg.ReadByte())
            {
                case GameMessageType.MOVEMENT:
                    {
                        int form = msg.ReadInt32();
                        Log.Info("Msg:MOVEMENT,from:" + form);
                        AvatarHandle avatar = FindPlayer(form);
                        if (avatar != null)
                        {
                            byte para = msg.ReadByte();
                            float y = msg.ReadFloat();
                            float z = msg.ReadFloat();
                            if (avatar != null)
                            {
                                avatar.Movement.LastState = DataHelp.GetOpetion(para);
                                avatar.Movement.UpdatePosition(y, z);
                            }
                        }
                        break;
                    }
                case GameMessageType.POSITION_REQUEST:
                    {
                        int form = msg.ReadInt32();
                        Log.Info("Msg:POSITION_REQUEST,from:" + form);
                        Owner.Movement.LastState[4] = Owner.Movement.Direction == MovementType.Left;
                        byte op = DataHelp.SetOpetion(Owner.Movement.LastState);
                        float y = Owner.Root.transform.position.y;
                        float z = Owner.Root.transform.position.z;

                        AppInterface.NetworkModule.SendNetworkMessage(ServerType.RegionServer, delegate(DataObject data)
                        {
                            data.WriteByte(GameMessageType.POSITION_RESPONSE);
                            data.WriteInt32(Owner.Id);
                            data.WriteInt32(form);
                            data.WriteByte(op);
                            data.WriteFloat(y);
                            data.WriteFloat(z);
                        });
                        break;
                    }
                case GameMessageType.POSITION_RESPONSE:
                    {
                        int form = msg.ReadInt32();
                        Log.Info("Msg:POSITION_RESPONSE,from:" + form);
                        AvatarHandle avatar = FindPlayer(form);
                        msg.ReadInt32();
                        byte para = msg.ReadByte();
                        float y = msg.ReadFloat();
                        float z = msg.ReadFloat();
                        if (avatar != null)
                        {
                            avatar.RemoteInit = true;
                            avatar.Movement.LastState = DataHelp.GetOpetion(para);
                            if (avatar.Movement.LastState[4]) avatar.Movement.Direction = MovementType.Left;
                            else avatar.Movement.Direction = MovementType.Right;
                            avatar.Movement.UpdatePosition(y, z);
                        }
                        break;
                    }
                case GameMessageType.ACTION:
                    {
                        int form = msg.ReadInt32();
                        Log.Info("Msg:ACTION,from:" + form);
                        AvatarHandle avatar = FindPlayer(form);
                        if (avatar != null)
                        {
                            avatar.Action.DoAction(msg.ReadInt16(), false);
                        }
                        break;
                    }
                case GameMessageType.ONBEAT:
                    {
                        msg.ReadInt32();
                        int form = msg.ReadInt32();
                        Log.Info("Msg:ONBEAT,from:" + form);
                        if (form == Owner.Id)
                        {
                            //Owner.Action.OnBeat(msg.ReadByte(), false);
                        }
                        else
                        {
                            AvatarHandle avatar = FindPlayer(form);
                            if (avatar != null)
                            {
                                //avatar.Action.OnBeat(msg.ReadByte(), false);
                            }
                        }
                        break;
                    }
            }
        }
    }
    #endregion
}

